<?php defined('SYSPATH') OR die('No direct access allowed.');

class Lokasi_Controller extends Controller {

    public function __construct() {
        parent::__construct();
        $this->lokasi_model = new Lokasi_Model;
    }

    public function index() {
		if(!parent::_authenticate()) {
			return;
		}
		parent::_loadContent("Lokasi Han Han", 'lokasi_content');        
        $this->view->content->lokasi = $this->lokasi_model;
		if(!empty($_POST)) {
			$this->validation = new Validation($_POST);
			$this->validation->add_rules('nama','required','length[1,32]');
			$this->validation->add_rules('alamat','required','standard_text');
			
			$this->validation->add_callbacks('nama',array($this,'_nama_check'));
			if($this->validation->validate()) {
				$this->lokasi_model->insert_lokasi(array('nama' => $this->input->post('nama'), 'alamat' => $this->input->post('alamat')));
			}else{
				$this->view->content->errors = $this->validation->errors('lokasi_error');				
			}
		}
        $this->view->render(TRUE);
    }
	
	public function _nama_check(Validation $post) {
		if(array_key_exists('nama',$post->errors())) {
			return ;
		}
		if ($this->lokasi_model->check_already_exist_lokasi($post->nama)) {
			$post->add_error('nama', '_nama_check');
		}
	}
}
